package com.phy.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {
	// 从一堆字符串中提取email地址
	// 使用动态数组
	public static ArrayList<String> ExtractEmail(String[] array) {
		// 如果是邮箱，进入动态数组
		String regex = "\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
		Pattern p = Pattern.compile(regex);
		ArrayList<String> emailList = new ArrayList<String>();
		for (int j = 0; j < array.length; j++) {
			Matcher m = p.matcher(array[j]);
			while (m.find()) {
				emailList.add(array[j]);
			}
		}
		return emailList;
	}

	// 从一堆字符串中提取有效域名地址
	// 使用动态数组
	public ArrayList<String> ExtractDNS(String[] DNSArray) throws IOException {
		String regex = "([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}";
		Pattern p = Pattern.compile(regex);
		ArrayList<String> DNSList = new ArrayList<String>();
		String hostname = "";
		for (int j = 0; j < DNSArray.length; j++) {
			Matcher m = p.matcher(DNSArray[j]);
			while (m.find()) {
				//System.out.println("开始的位置是:" + hostname.indexOf("www."));
				hostname = m.group();
				if (hostname.startsWith("www.")) {
					hostname = hostname.substring(4);
				}
				//System.out.println("新的字符串是:" + hostname);
				DNSList.add(hostname);
			}
		}
		return DNSList;
	}
	//转义输出
	public String StrChange(String str) {
		String result = str;
		result=result.replaceAll("<+","&lt;");
		result=result.replaceAll(">+", "&gt;");
		return result;
	}
}
